<?php
/**
 * Application model for Cake.
 *
 * This file is application-wide model file. You can put all
 * application-wide model-related methods here.
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Model
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

App::uses('Model', 'Model');

/**
 * Application model for Cake.
 *
 * Add your application-wide methods in the class below, your models
 * will inherit them.
 *
 * @package       app.Model
 */
class AppModel extends Model {
	public function unique( $data_field=array(), $unique_field=null){
		$this->recursives=0;
		$function_name=explode('::',__METHOD__);
		$function_name=$function_name[1];
		
		$data=null;
		$conditions=array();
		$tranlate_unique_field=$unique_field;
		//get translate table
		$translateTable='i18n';
		if(!empty($this->{$model}->translateTable))$translateTable=$this->{$model}->translateTable;
		$translateModel=Inflector::camelize($translateTable);
		
		//buil condition
		if(isset($this->actsAs['Translate']) && is_array($this->actsAs['Translate']) && in_array($unique_field,$this->actsAs['Translate']))
		{			
			$tranlate_unique_field=$translateModel.'__'.$tranlate_unique_field.'.content';
			$conditions[$translateModel.'__'.$unique_field.'.locale']=$this->locale;
		}
		else{
			$tranlate_unique_field=$this->name.'.'.$tranlate_unique_field;
		}
		//same name
		$conditions[$tranlate_unique_field]=$data_field[$unique_field];
		//different id
		$conditions[$this->name.'.id <>']=$this->id;
		$data=$this->find('first',array('conditions'=>$conditions));
		if(empty($data))return true;		
		return false;		
	}
	public function uniqueTree( $data_field=array(), $unique_field=null,$parent_field='parent_id'){
		$this->recursives=0;
		$function_name=explode('::',__METHOD__);
		$function_name=$function_name[1];
		$data=null;
		$conditions=array();
		$tranlate_unique_field=$unique_field;
		//get translate table
		$translateTable='i18n';
		if(!empty($this->{$model}->translateTable))$translateTable=$this->{$model}->translateTable;
		$translateModel=Inflector::camelize($translateTable);
		//build condition
		if(isset($this->actsAs['Translate']) && is_array($this->actsAs['Translate']) && in_array($unique_field,$this->actsAs['Translate']))
		{			
			$tranlate_unique_field=$translateModel.'__'.$tranlate_unique_field.'.content';
			$conditions[$translateModel.'__'.$unique_field.'.locale']=$this->locale;
		}
		else{
			$tranlate_unique_field=$this->name.'.'.$tranlate_unique_field;
		}
		//same name
		$conditions[$tranlate_unique_field]=$data_field[$unique_field];
		//same parent_id
		$conditions[$this->name.'.'.$parent_field]=intval($this->data[$this->name][$parent_field]);
		//different id
		$conditions[$this->name.'.id <>']=$this->id;
		//t($this->data);
		$data=$this->find('first',array('conditions'=>$conditions));
		if(empty($data))return true;		
		return false;		
		
	}
// using app/Model/ProductCategory.php
	// In the following example, do not let a product category be deleted if it still contains products.
	// A call of $this->Product->delete($id) from ProductsController.php has set $this->id .
	// Assuming 'ProductCategory hasMany Product', we can access $this->Product in the model.
	public function beforeDelete($cascade = true) {
		//unbound
		App::uses('FileManager', 'Model');
		$FileManager= new FileManager();
		$data=$this->findById($this->id);	
		if(isset($data[$this->name]['image']))			
			$FileManager->updateAll(array('mount'=>'mount - 1'),array('name'=>$data[$this->name]['image']));
		
	   return true;
	}
}
